var express = require('express');
var router = express.Router();
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/chatUser");
mongoose.Promise = Promise;
const Schema = mongoose.Schema;
const User = mongoose.model("User", userSchema);

// 建立 User 类
var userSchema = new Schema({
  name: String,
  password: String,
  age: Number
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

router.get('/register', function (req, res) {
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  res.render('register');
});

router.get('/login', function (req, res) {
  res.locals.firstnum = 2;
  res.locals.secondnum = 3;
  res.render('login');
});

router.post('/register', function (req, res) {
  const {
    loginname,
    password,
    confirm,
    vnum
  } = req.body;
  console.log(loginname);
  let plus = Number(vnum) === req.session.firstnum + req.session.secondnum ? true : false;
  console.log("plus: " + plus + " " + Number(vnum) + " " + req.session.firstnum + req.session.secondnum);
  let leo = new User({
    name: loginname,
    password: password,
    age: 18
  });
});

module.exports = router;
